﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using DAL;
using BL;

namespace QuanLyCHTrangSuc
{
    public partial class QuanLy_User : Form
    {
        List<QuanLyUserDAL> dsUser = new List<QuanLyUserDAL>();
        QuanLyUserDAL us = new QuanLyUserDAL();
        public QuanLy_User()
        {
            InitializeComponent();
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            dsUser = QuanLyUserBL.LayDanhSachUser();
            if (dsUser.Count == 0)
            {
                MessageBox.Show(" Dữ liệu không tồn tại để thực hiện chức năng xóa này!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DialogResult XOA = MessageBox.Show(" Bạn thực sự muốn xóa user " + us.TaiKhoan + "?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (XOA == DialogResult.Yes)
                {
                    QuanLyUserBL.XoaUser(us.TaiKhoan);
                    lv_dsUser.Items.Remove(this.lv_dsUser.SelectedItems[0]);
                    clear_textbox();
                }
                else
                {
                    MessageBox.Show(" Bạn đã không xóa!", " Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void QuanLy_User_Load(object sender, EventArgs e)
        {
            LayDanhSachUser();
        }

        protected void LayDanhSachUser()
        {
            dsUser = QuanLyUserBL.LayDanhSachUser();

            if (dsUser.Count == 0)
            {
                MessageBox.Show(" Danh sách rỗng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            for (int i = 0; i < dsUser.Count; ++i)
            {

                ListViewItem lvi = new ListViewItem();

                lvi.Text = dsUser[i].TaiKhoan;
                lvi.SubItems.Add(dsUser[i].MatKhau);
                lvi.SubItems.Add(dsUser[i].MaQuyen);
                lvi.SubItems.Add(dsUser[i].HoTen);
                lvi.SubItems.Add(dsUser[i].DiaChi);
                lvi.SubItems.Add(dsUser[i].Email);
                lvi.SubItems.Add(dsUser[i].SoDienThoai);
                lvi.SubItems.Add(dsUser[i].DiemtichLuy.ToString());
                lvi.SubItems.Add(dsUser[i].GhiChu);
                lvi.SubItems.Add(dsUser[i].GiaTriTK.ToString());
                lvi.Tag = (object)dsUser[i].TaiKhoan;


                lv_dsUser.Items.Add(lvi);
            }
        }

        private void lv_dsUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lv_dsUser.SelectedItems.Count > 0)
            {
                ListViewItem lvi = this.lv_dsUser.SelectedItems[0];

                // gan' thuoc tinh tu lvi qua us               
                us.TaiKhoan = lvi.SubItems[0].Text;
                us.MatKhau = lvi.SubItems[1].Text;
                us.MaQuyen = lvi.SubItems[2].Text;
                us.HoTen = lvi.SubItems[3].Text;
                us.DiaChi = lvi.SubItems[4].Text;
                us.Email = lvi.SubItems[5].Text;
                us.SoDienThoai = lvi.SubItems[6].Text;
                us.DiemtichLuy = Convert.ToInt32(lvi.SubItems[7].Text);
                us.GhiChu = lvi.SubItems[8].Text;
                us.GiaTriTK = Convert.ToInt32(lvi.SubItems[9].Text);

                //gán thuộc tính từ lvi qua textbox
                tb_TaiKhoan.Text= lvi.SubItems[0].Text;
                tb_MatKhau.Text = lvi.SubItems[1].Text;
                cb_Quyen.Text = lvi.SubItems[2].Text;
                tb_HoTen.Text = lvi.SubItems[3].Text;
                tb_DiaChi.Text = lvi.SubItems[4].Text;
                tb_Email.Text = lvi.SubItems[5].Text;
                tb_SoDienThoai.Text = lvi.SubItems[6].Text;
                tb_DiemTichLuy.Text = lvi.SubItems[7].Text;
                tb_GhiChu.Text = lvi.SubItems[8].Text;
                tb_GiaTriTK.Text = lvi.SubItems[9].Text;
            }
        }

       

        private void TimUer()
        {
            lv_dsUser.Items.Clear();
            List<QuanLyUserDAL> dsUser = QuanLyUserBL.TimUser(tb_Tim.Text);
            if(dsUser.Count==0)
                MessageBox.Show(" Không tìm thấy !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(" Đã tìm thấy !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            for (int i = 0; i < dsUser.Count; ++i)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = dsUser[i].TaiKhoan;
                lvi.SubItems.Add(dsUser[i].MatKhau);
                lvi.SubItems.Add(dsUser[i].MaQuyen);
                lvi.SubItems.Add(dsUser[i].HoTen);
                lvi.SubItems.Add(dsUser[i].DiaChi);
                lvi.SubItems.Add(dsUser[i].Email);
                lvi.SubItems.Add(dsUser[i].SoDienThoai);
                lvi.SubItems.Add(dsUser[i].DiemtichLuy.ToString());
                lvi.SubItems.Add(dsUser[i].GhiChu);
                lvi.SubItems.Add(dsUser[i].GiaTriTK.ToString());
                lvi.Tag = (object)dsUser[0].TaiKhoan;
                //giu lai de su dung, khi can thi lay ra
                lv_dsUser.Items.Add(lvi);
            }
        }

       

        private void bt_Tim_Click(object sender, EventArgs e)
        {
            TimUer();
        }

        private void bt_Xem_Click(object sender, EventArgs e)
        {
            lv_dsUser.Items.Clear();
            LayDanhSachUser();
        }

        private void bt_Thoat_Click(object sender, EventArgs e)
        {
            DialogResult thoat = MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (thoat == DialogResult.Yes)
            Close();
        }

        public void clear_textbox()
        {
            tb_TaiKhoan.Text = "";
            tb_MatKhau.Text = "";
            cb_Quyen.Text = "";
            tb_HoTen.Text = "";
            tb_DiaChi.Text = "";
            tb_Email.Text = "";
            tb_SoDienThoai.Text = "0";
            tb_GhiChu.Text = "";
            tb_DiemTichLuy.Text = "0";
            tb_GiaTriTK.Text = "0";
        }
        
        private void bt_Them_Click(object sender, EventArgs e)
        {
            if (tb_TaiKhoan.Text == "" || tb_MatKhau.Text == "" || cb_Quyen.Text == "")
            {
                MessageBox.Show("Không được để trống Tài Khoản, Mật Khẩu hay Quyền!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (!IsValidEmail(tb_Email.Text.Trim()))
            {
                MessageBox.Show("Email không hợp lệ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            QuanLyUserBL.ThemUserNguoiDung(tb_TaiKhoan.Text, tb_MatKhau.Text, cb_Quyen.Text, tb_HoTen.Text, tb_DiaChi.Text, tb_Email.Text, tb_SoDienThoai.Text, Convert.ToInt32(tb_GiaTriTK.Text));
            QuanLyUserBL.ThemUserNguoiDungCuoi(tb_TaiKhoan.Text, int.Parse(tb_DiemTichLuy.Text), tb_GhiChu.Text);
            lv_dsUser.Items.Clear();
            LayDanhSachUser();
        }

        public bool IsValidEmail(string email)
        {
            try
            {
                int nFirt = int.Parse(email.Substring(0, 1));
                return false;
            }
            catch
            {
                string pattern = @"^[_a-zA-Z0-9][_.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[_.a-zA-Z0-9]+)*\.
    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|vn|[a-zA-Z]{2})$";
                //Regular expression object
                Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
                //boolean variable to return to calling method
                bool valid = false;

                //make sure an email address was provided
                if (string.IsNullOrEmpty(email))
                {
                    valid = false;
                }
                else
                {
                    //use IsMatch to validate the address
                    valid = check.IsMatch(email.ToLower());
                }
                //return the value to the calling method
                return valid;
            }
        }

        private void bt_NhapLai_Click(object sender, EventArgs e)
        {
            clear_textbox();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (tb_MatKhau.Text == "" || cb_Quyen.Text == "")
            {
                MessageBox.Show("Không được để trống Mật Khẩu hay Quyền!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            try
            {
                float gttk = float.Parse(tb_GiaTriTK.Text);
                QuanLyUserBL.SuaUserNguoiDung(us.TaiKhoan, tb_MatKhau.Text, cb_Quyen.Text, tb_HoTen.Text, tb_DiaChi.Text, tb_Email.Text, tb_SoDienThoai.Text, gttk);
                QuanLyUserBL.SuaUserNguoiDungCuoi(us.TaiKhoan, int.Parse(tb_DiemTichLuy.Text), tb_GhiChu.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Sửa Thất Bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            lv_dsUser.Items.Clear();
            LayDanhSachUser();
        }

        private void tb_SoDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;	
        }


        private void tb_DiemTichLuy_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void tb_GiaTriTK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}
